/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2008-2011, Salzburg Research. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * - Neither the name of the LMF library client Project nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributor(s):
 * Stephanie Stroka
 *
 * File Info:
 * User: Stephanie Stroka
 * Date: 2011/08/19
 * Time: 18:04
 *
 * Project: lmflib
 */

package kiwi.facading.utils;

import kiwi.facading.exceptions.NamespaceResolvingException;
import kiwi.facading.model.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sstroka
 *         User: sstroka
 *         Date: 16.08.2011
 *         Time: 19:11:36
 */
public class KiWiNamespaceUtils {

    private final static Logger log =  LoggerFactory.getLogger(KiWiNamespaceUtils.class);

    private final static Map<String,String> prefix_namespace = new HashMap<String,String>();
    static {
        prefix_namespace.put("rdf", Constants.NS_RDF);
        prefix_namespace.put("rdfs", Constants.NS_RDFS);
        prefix_namespace.put("kiwi", Constants.NS_KIWI_CORE);
        prefix_namespace.put("dc", Constants.NS_DC);
        prefix_namespace.put("foaf", Constants.NS_FOAF);
        prefix_namespace.put("skos", Constants.NS_SKOS);
        prefix_namespace.put("geo", Constants.NS_GEO);
        prefix_namespace.put("owl", Constants.NS_OWL);
        prefix_namespace.put("atom", Constants.NS_ATOM);
        prefix_namespace.put("bio", Constants.NS_BIO);
        prefix_namespace.put("sioc", Constants.NS_SIOC);
        prefix_namespace.put("xsd", Constants.NS_XSD);
        prefix_namespace.put("xml", Constants.NS_XML);
        prefix_namespace.put("xhtml", Constants.NS_XHTML);
    }

    public static String getNamespace(String prefix) {
        return prefix_namespace.get(prefix);
    }

    public static boolean hasNamespace(String prefix) {
        if(getNamespace(prefix) != null)
            return true;
        return false;
    }

    public static String resolvePropLabel(String propLabel) throws NamespaceResolvingException {
        String uri = propLabel;

        // find out which kind of propLabel we got passed
        if(uri.startsWith("<") && uri.endsWith(">")) {
            // uri is a real uri enclosed in < >
            uri = uri.substring(1,uri.length()-1);
        } else if(!uri.contains("://") && uri.contains(":")) {
            // uri is a SeQRQL/SPARQL identifier with abbreviated namespace, we need to lookup the namespace...
            String[] components = uri.split(":");
            if(components.length == 2) {
                String ns_prefix = components[0];
                String ns_local  = components[1];

                if(hasNamespace(ns_prefix)) {
                    uri = getNamespace(ns_prefix);
                }
                uri = uri + ns_local;
            } else {
                log.error("could not properly split property identifier #0, as it contained more than one ':'",uri);
            }
        }
        return uri;
    }
}
